<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <style>
        *{
    margin: 0;
    padding: 0; 
}
#BC{
    height: 100vh;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.4);
    z-index: 0;
    opacity: 0;
    position: absolute;
}
.BackGround-Color{
    width: 100%;
    height: 100vh;
    margin-bottom: auto;
    background-image: linear-gradient(180deg,#e66464c4,#9198e5bd);
    display: flex;
    align-items: center;
    justify-content: center;
}
/* 街机模式css样式 */
.Game-Title{
    position: relative;
    top: 20px;
    text-align: center;
    font-size:x-large;
    z-index: 2;
    cursor: pointer;
    color: rgba(255, 0, 0);
}
.Game-Container{
    background-color: rgba(0, 255, 255, 0.3);
    width: 400px;
    height: 750px;
    overflow: hidden;
}
.Line-Block{
    animation: Move;
    position: relative;
    width: 400px;
    height: 150px;
    margin-bottom: 3px;
}
.Line-Block span{
    display: block;
    float: left;
    cursor: pointer;
    width: 98px;
    height: 150px;
    border: 1px solid rgba(0, 0, 0, 0.6);
    transform: translateY(-60px);
}
#Game-Start{
    text-align: center;
    line-height: 150px;
    font-size: 20px;
    color:white;
}
.Line-Block span:hover{
    box-shadow:0px 0px 3px azure;
}

.Black-Block{
    background-color: rgba(0, 0, 0, 0.5);
}
.Grey-Block{
    background-color: rgba(128, 128, 128, 0.3);
}
.Red-Block{
    background-color: red;
}
.Score{
    position: absolute;
    right: 20px;
    top: 10px;
}
.btn1{
    margin: 20px;
    width: 100px;
    height: 40px;
    position: relative;
    left: 180px;
    border-radius: 10px;
    background-image: linear-gradient(rgba(255, 255, 255, 0.5),cyan);
    text-decoration: none;
    text-align: center;
    line-height: 40px;
    display: block;
    border: 2px solid blueviolet;
}
.btn1:hover{
    transform: scale(1.2);
    transition: all 0.5s;
}
.Time{
    position: absolute;
    left: 20px;
    top: 10px;
    width: 150px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    color: white;
    font-size: 30px;
    border: 3px solid black;
}
#Loser, #Finish{
    display: flex;
    flex-direction: column;
    position: absolute;
    top: 200px;
    width: 500px;
     height: 00px; /**/
    text-align: center;
    line-height: 100px;
    border-radius: 20px;
    background-color: white;
    overflow: hidden;
    
}
#Loser h3{
    line-height: 30px;
}
#Finish h3{
    line-height: 30px;
}
#Loser a{
    margin: 20px;
    width: 100px;
    height: 40px;
    border-radius: 10px;
    background-image: linear-gradient(rgba(255, 255, 255, 0.5),cyan);
    text-decoration: none;
    text-align: center;
    line-height: 40px;
    display: block;
    border: 2px solid blueviolet;
}
#Finish a{
    margin: 20px;
    width: 100px;
    height: 40px;
    border-radius: 10px;
    background-image: linear-gradient(rgba(255, 255, 255, 0.5),cyan);
    text-decoration: none;
    text-align: center;
    line-height: 40px;
    display: block;
    border: 2px solid blueviolet;
}
#Loser button:hover{
    transform: scale(1.11);
    font-size: medium;
    transition: all 0.3s;
}
@keyframes Move {
    0%{
        transform: translateY(-150px);
    }
    50%{
        transform: translateY(500px);
    }
    100%{
        transform: translateY(1000px);
    }
}
    </style>
    <title>街舞模式</title>
</head>
<body>
    <div id="BC"></div>
    <div class="BackGround-Color">
        <div class="Game-Container">
            <div class="Game-Title">
                <h2>街舞模式 </h2>
            </div>
            <div id="Game-Line">
                <div class="Line-Block">
                    <span class="White-Block"></span>
                    <span class="Black-Block"></span>
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                </div>
                <div class="Line-Block">
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                    <span class="Black-Block"></span>
                    <span class="White-Block"></span>
                </div>
                <div class="Line-Block">
                    <span class="Black-Block"></span>
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                </div>
                <div class="Line-Block">
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                    <span class="Black-Block" id="Game-Start">开始</span>
                </div>
                <div class="Line-Block">
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                    <span class="White-Block"></span>
                </div>
            </div>
        </div>
        <div class="Score">
            <strong style="font-size: 30px;color: white;">你的得分是:<span id="scores">0</span></strong>
        </div>
        <div class="Time">
            <div id="timer"></div>
        </div>
        <div id="Loser">
            <strong>YOU LOSE</strong>
            <h3>你的得分是:<p id="score">0</p></h3>
            <a href="./街机模式.html" class="btn1">重新开始</a>
        </div>
        <div id="Finish">
            <strong>Time Out</strong>
            <h3>你的得分是:<p id="TOscore">0</p></h3>
            <a href="./街机模式.html" class="btn1">重新开始</a>
        </div>
    </div>
</body>
</html>
<script>
    //无限获得方块
function CreateBlock(){
    var Lines = document.createElement('div');
    Lines.className='Line-Block'
    var i = Math.floor(Math.random()*4);
    for (let index = 0; index < 4; index++) {
        
        if(index==i){
            var Black_Block = document.createElement('span');
            Black_Block.className='Black-Block';
            Lines.appendChild(Black_Block);
        }
        else{
            var White_Block = document.createElement('span');
            White_Block.className='White-Block';
            Lines.appendChild(White_Block);
        }
    }


    //将获得的方块添加到行
    var Game_Line = document.getElementById('Game-Line');
    Game_Line.insertBefore(Lines,Game_Line.childNodes[0]);
    BlackBottom();
    //让行向下移动
    var Container = document.getElementById('Game-Line');
    Container.style.transform='Move 0.5s linear infinite';
    //刷新点击事件让新产生的格子获得属性
    ClickBlock();
}

//计算黑色格子与底部的距离
function BlackBottom() {
    var Black_Block = document.getElementsByClassName('Black-Block');
    for (let index = 0; index < Black_Block.length; index++) {
        const element = Black_Block[index];
        if(element.offsetTop>710){
            //如果黑色格子触底则游戏结束
            let Lose = document.getElementById('Loser');
            Lose.style.height="300px"
            Lose.style.transition="all 0.5s"
            Lose.style.zIndex='4';

        }
    }

    //白色格子掉落底部后，删除该行节点

    // var Points =document.getElementById('Game-Line')
    // var Grey_Block = document.getElementsByClassName('White-Block');
    // for (let i = 0; i < Grey_Block.length; i++) {
    //     const elements = Grey_Block[i];
    //     if(elements.offsetTop>710){
    //         Points.removeChild(Points.childNodes[Grey_Block.length]);
    //     }
    // }
    
} 
GameStart()
function GameStart() {
    var Start = document.getElementById('Game-Start')
    Start.addEventListener('click',function Begin(){
    Start.className='Grey-Block'; 
    CreateBlock()
    setInterval(function () {
         timer.innerHTML=showtime();
        },1000);
    })
}

//格子点击事件
function ClickBlock() {
    let score = '0';
    var Block = document.getElementsByTagName('span');
    for (let index = 0; index < Block.length; index++) {
        const element = Block[index];
        if(element.className=='Black-Block'){
        element.addEventListener('click',function(){
            console.log(score);
            element.className='Grey-Block';
            var Black_Block = document.getElementsByClassName('Grey-Block');
            console.log(Black_Block.length);
            var scores = document.getElementById('scores');
            scores.innerHTML=Black_Block.length;
            var sc = document.getElementById('score');
            sc.innerHTML=Black_Block.length;
            let Tscore = document.getElementById('TOscore');
            Tscore.innerHTML=Black_Block.length
            CreateBlock();
        })
        }else if(element.className=='White-Block'){
            element.addEventListener('click',function () {
                element.className='Red-Block';
                let Lose = document.getElementById('Loser');
                Lose.style.height="300px"
                Lose.style.transition="all 0.5s"
                Lose.style.zIndex='6';
                var BC = document.getElementById('BC');
                BC.style.opacity='1';
                BC.style.zIndex='3';
            })
        };
    }
    }



//计时函数
var timer = document.getElementById('timer');
timer.innerHTML='20秒'
let seconds = '20';
function showtime() {
    if(seconds>0){
    seconds=seconds-1;

    return seconds + "秒";
}
    else{
        let Fi = document.getElementById('Finish');
                Fi.style.height="300px"
                Fi.style.transition="all 0.5s"
                Fi.style.zIndex='5';
                var BC = document.getElementById('BC');
                BC.style.opacity='1';
                BC.style.zIndex='3';

        return 0+"秒";
    }
}
</script>